package com.dongjiantong.quarantine.serviceImpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.biz.enums.ReportType;
import com.dongjiantong.biz.model.EarMarkLife;
import com.dongjiantong.biz.model.ReportHandleResumes;
import com.dongjiantong.biz.service.EarMarkLifeWriteService;
import com.dongjiantong.biz.service.ReportHandleResumesService;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.quarantine.enums.FileType;
import com.dongjiantong.quarantine.enums.HarmlessStatus;
import com.dongjiantong.quarantine.mapper.BreedReportsFileMapper;
import com.dongjiantong.quarantine.mapper.HarmlessMapper;
import com.dongjiantong.quarantine.model.BreedReportsFile;
import com.dongjiantong.quarantine.model.Harmless;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 23/11/17.
 */
@Slf4j
@Service(version = "1.0.0")
public class HarmlessWriteServiceImpl extends ServiceMybatis<Harmless> implements HarmlessWriteService {

    @Resource
    private HarmlessMapper harmlessMapper;

    @Resource
    private BreedReportsFileMapper breedReportsFileMapper;

    @Reference(version = "1.0.0")
    private EarMarkLifeWriteService earMarkLifeWriteService;

    @Reference(version = "1.0.0")
    private ReportHandleResumesService reportHandleResumesService;

    /**
     * 无害化更新
     *
     * @param harmless 无害化单据对象
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Override
    public Map<String, Object> update(Harmless harmless, Integer toStatus,LoginInfo loginInfo) {
        Map<String, Object> maps = Maps.newHashMap();
        Integer fromStatus = harmless.getStatus();
        Boolean isSuccess = false;
        if (toStatus != null && !toStatus.equals(fromStatus)) {
            harmless.setStatus(toStatus);
            isSuccess = harmlessMapper.updateByPrimaryKey(harmless) == 1;
            ReportHandleResumes reportHandleResumes = new ReportHandleResumes();

            reportHandleResumes.setFrom_status(fromStatus.byteValue());
            reportHandleResumes.setReport_id(harmless.getId());
            reportHandleResumes.setReport_number(harmless.getNumber());
            reportHandleResumes.setTo_status(toStatus.byteValue());
            reportHandleResumes.setType(ReportType.QUARANTINE_ORIGIN_AREA.value());
            reportHandleResumes.setCreated_at(new Date());
            reportHandleResumes.setUpdated_at(new Date());

            reportHandleResumesService.create(reportHandleResumes);
            if (toStatus.equals(HarmlessStatus.STAY_APPROVE.value())) {
                List<String> harmless_photos = harmless.getHarmless_photos();
                List<String> harmless_videos = harmless.getHarmless_videos();
                List<String> harmless_video_snapshoots = harmless.getHarmless_video_snapshoots();
                if (harmless_photos != null && !harmless_photos.isEmpty()) {
                    writeFiles(ReportType.HARMLESS.value(), FileType.PHOTO.value(), harmless.getId(), harmless_photos, "harmless_photos", "无害化照片");
                }
                if (harmless_videos != null && !harmless_videos.isEmpty()) {
                    writeFiles(ReportType.HARMLESS.value(), FileType.VIDEO.value(), harmless.getId(), harmless_videos, "harmless_videos", "无害化视频");
                }
                if (harmless_video_snapshoots != null && !harmless_video_snapshoots.isEmpty()) {
                    writeFiles(ReportType.HARMLESS.value(), FileType.PHOTO.value(), harmless.getId(), harmless_video_snapshoots, "harmless_video_snapshoots", "无害化视频快照");
                }
            }
            if (toStatus.equals(HarmlessStatus.STAY_APPROVE.value())) {
                insertToEarLife(harmless,loginInfo);
                updateOtherQuarantineEarLifeSelected(harmless);
            }
        } else {
            isSuccess = harmlessMapper.updateByPrimaryKey(harmless) == 1;
        }

        maps.put("isSuccess", isSuccess);
        return maps;
    }

    /**
     * 更新无害化的delete-flag
     *
     * @param updateHarmlessParamMap 参数
     */
    @Override
    public void updateDeleteFlag(Map<String, Object> updateHarmlessParamMap) {
        harmlessMapper.updateDeleteFlag(updateHarmlessParamMap);
    }


    /**
     * 图片或文件的写入
     *
     * @param fileUrls 照片的url
     * @param field    字段名
     * @param state    意义
     */
    private void writeFiles(Integer type1, Integer type2, Long relevanceId, List<String> fileUrls, String field, String state) {
        if (fileUrls != null && !fileUrls.isEmpty()) {
            for (String string : fileUrls) {
                BreedReportsFile breedReportsFile = new BreedReportsFile();
                breedReportsFile.setType1(type1);
                breedReportsFile.setType2(type2);
                breedReportsFile.setField(field);
                breedReportsFile.setAddress(string);
                breedReportsFile.setRelevance_id(relevanceId);
                breedReportsFile.setState(state);
                breedReportsFile.setDate(new Date());
                breedReportsFileMapper.insert(breedReportsFile);
            }
        }
    }

    /**
     * 耳标使用记录的写入
     *
     * @param harmless 无害化对象
     */
    private void insertToEarLife(Harmless harmless, LoginInfo loginInfo) {
        List<String> earTagNumbers = harmless.getEar_tag_numbers();
        if (earTagNumbers != null && !earTagNumbers.isEmpty()) {
            List<EarMarkLife> earMarkLives = null;
            for (String earTagNumber : earTagNumbers) {
                earMarkLives = new ArrayList<>();
                EarMarkLife earMarkLife = new EarMarkLife();

                earMarkLife.setMark_number(earTagNumber);
                earMarkLife.setReport_id(harmless.getId());
                earMarkLife.setReport_name(harmless.getNumber());
                earMarkLife.setReport_type(3);
                earMarkLife.setMark_type(harmless.getAnimal_type());
                earMarkLife.setOperator_id(loginInfo.getId());
                earMarkLife.setOperator_name(loginInfo.getRealName());
                earMarkLife.setIs_selected(1);
                earMarkLife.setCreated_at(new Date());

                earMarkLives.add(earMarkLife);
            }
            earMarkLifeWriteService.insertList(earMarkLives);
        }
    }

    /**
     * 更爱耳标记录从单据的耳标的选择状态
     *
     * @param harmless 无害化对象
     */
    private void updateOtherQuarantineEarLifeSelected(Harmless harmless) {
        List<String> earTagNumbers = harmless.getEar_tag_numbers();

        if (earTagNumbers != null && !earTagNumbers.isEmpty()) {
            Map<String, Object> map = Maps.newHashMap();

            map.put("isSelected", 2);
            map.put("earTagNumbers", earTagNumbers);
            map.put("reportId", harmless.getReport_id());
            map.put("reportType", harmless.getReport_type());
            map.put("markType", harmless.getProduct_type());

            earMarkLifeWriteService.updateMarks(map);
        }
    }

}
